<?php


namespace App\Http\Controllers\Api;


use App\Services\Business\Models\Image;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Requests\ApiV2\ImageUploadRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;


class ImageController extends BaseController
{

    /**
     * 图片上传
     * @param ImageUploadRequest $request
     * @return JsonResponse
     */
    public function upload(ImageUploadRequest $request)
    {


        $folder = $request->input('folder', 'default');
        $file = $request->filldata();
        $prefix = $folder . '/' . date("Ymd");
        $data = save_image($file, $prefix);
        return $this->successData(compact('data'));
    }

    /**
     * 删除阿里云oss的资源
     * @param Request $request
     * @return JsonResponse
     */
    public function delete(Request $request,$imageId)
    {

        $image =  Image::query()->where('id',$imageId)->first();
        Storage::disk('oss')->delete([$image->path, $image->path_small]);
        return $this->success(__('删除图片成功'));
    }

    /**
     * 删除oss及数据库的记录
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function destroy(Request $request, $id)
    {

        $image = Image::find($id);
        Storage::disk(config('zesow.upload.image.disk'))->delete($image->path);
        $image->delete();
        return $this->success(__('删除图片成功'));
    }

}
